package com.xxl.job.executor.service;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.kafka.KafkaProduceService;
import com.xxl.job.executor.model.CurrentInfoXfModel;
import com.xxl.job.executor.model.SignalDefHkModel;
import com.xxl.job.executor.model.SignalDefXfModel;
import com.xxl.job.executor.repository.CurrentInfoRepository;
import com.xxl.job.executor.utils.ThreadLocalDateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class H3CApCollectService {

    private static final Logger LOGGER = LoggerFactory.getLogger(H3CApCollectService.class);


    @Autowired
    private SignalDefService signalDefService;
    @Autowired
    private CurrentInfoRepository currentInfoRepository;
    @Autowired
    private KafkaProduceService kafkaProduceService;

    public void save(SignalDefXfModel automaticControlModel, int flag){
        SignalDefHkModel signalDefHkModel=signalDefService.queryHkSignal(
                automaticControlModel.getGatherId(), automaticControlModel.getGatherResId(),automaticControlModel.getPointAddr());
        if(signalDefHkModel==null){
            LOGGER.error("自控系统：未找到对应的信号配置");
            return;
        }

        String nDate="";
        nDate= ThreadLocalDateUtil.formatDate(new Date());
//        String signalType  =signalDefXfModel.getSignalType();
//
//        //新建实时存储的实体
//        CurrentInfoXfModel currentInfoXfModel=new CurrentInfoXfModel();
//        currentInfoXfModel.setValue(model.getSignalValue());
//        currentInfoXfModel.setGatherResId(model.getGatherResId());
//        currentInfoXfModel.setGatherId(model.getGatherId());
//        currentInfoXfModel.setDevCode(signalDefXfModel.getDevCode());
//        currentInfoXfModel.setSignalCode(signalDefXfModel.getCode());
//        currentInfoXfModel.setSignalName(signalDefXfModel.getName());
//        currentInfoXfModel.setSignalType(signalDefXfModel.getSignalType());
//        currentInfoXfModel.setValunit(signalDefXfModel.getValunit());
//        currentInfoXfModel.setTime(nDate);
//        currentInfoXfModel.setPointAddr(signalDefXfModel.getPointAddr());
//
//        //根据实体去redis查询上次信号时间
//        CurrentInfoXfModel currentInfoXfModel1=currentInfoRepository.queryXfCurrent(signalDefXfModel.getDevCode(),signalDefXfModel.getCode());
//
//        if(currentInfoXfModel1==null){
//            currentInfoXfModel.setLastTime(nDate);
//        }else{
//            currentInfoXfModel.setLastTime(currentInfoXfModel1.getTime());
//        }
//        currentInfoXfModel.setSendTime(ThreadLocalDateUtil.formatDate(new Date()));
//
//        //实时数据存入redis
//        currentInfoRepository.save(currentInfoXfModel);
//
//        //发送kafka
//        send(currentInfoXfModel);
    }

    /**
     * 发kafka
     * @param message
     */
    public void send(CurrentInfoXfModel message) {

        try {
            this.kafkaProduceService.sendXfAlarmMessage(message);

        } catch (Exception e) {
            XxlJobLogger.log("发送告警信息失败:"+message.toString(), e);
        }
    }
}
